
package net.vinote.smart.platform.service.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.module.database.BeanEntityFactory;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.Table;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.database.bo.DBQuery;
import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.bean.AppItem;
import net.vinote.smart.platform.service.util.AutoInjection;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;


/**
 * 子系统Action类
 * 
 * @author 郑俊伟
 */
public class AppItemAction
{
	@AutoInjection
	private String appID;

	@AutoInjection
	private String itemID;

	/**
	 * 删除一条子系统栏目记录
	 * 
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_APP_ITEM_DELETE)
	public String deleteAppItem() throws SQLException
	{
		// 第一步:清除权限配置信息
		DBManager bm = DBManagerFactory.getInstance("role_object_rela");
		bm.deleteWhereSql("objectType='AppItem' and objectID=?", itemID);
		// 第二步:删除app_item信息
		bm = DBManagerFactory.getInstance("app_item");
		int result = bm.delete(new String[]
		{
				"appID", "itemID"
		}, new String[]
		{
				appID, itemID
		});
		return result == 1 ? PlatformConst.SUCCESS : PlatformConst.FAIL;
	}

	/**
	 * 获取子系统的所有栏目
	 * 
	 * @param appID
	 * @return
	 * @throws SQLException
	 */
	public List<AppItem> getAppItems(String appID) throws SQLException
	{
		List<AppItem> itemList = new ArrayList<AppItem>();
		DBManager bm = DBManagerFactory.getInstance("app_item");
		List<BizObject> list = bm.createQuery(
				SRE.getAttribute(PlatformConst.SRE_PREFIX_TABLE + "app_item",
						Table.class).getFieldsName(),
				"appID='" + appID + "' and isInuse='1' order by sortNo")
				.getResultList();
		for (BizObject bo : list)
		{
			AppItem item = new AppItem();
			item.setItemID(bo.getAttribute("ItemID"));
			item.setAppID(bo.getAttribute("AppID"));
			item.setItemName(bo.getAttribute("ItemName"));
			item.setUrl(bo.getAttribute("URL"));
			itemList.add(item);
		}
		return itemList;
	}

	/**
	 * 根据用户角色获取可用的应用栏目
	 * 
	 * @param appID
	 * @param userID
	 * @return
	 */
	public List<AppItem> getAppItemsByUserRoles(String appID, String userID)
	{
		DBManager bm = DBManagerFactory.getInstance("app_item");
		DBQuery query = null;
		List<AppItem> itemList = new ArrayList<AppItem>();
		try
		{
			query = bm
					.createQuery(
							SRE.getTable("app_item").getFieldsName(),
							"O.APPID=? and O.ISINUSE='1' and EXISTS(select 1 from role_object_rela ror where ror.objectType='AppItem' and ror.objectid=O.ITEMID and exists (select 1 from user_role_rela urr where urr.roleid=ror.roleID and urr.userid=?)) order by O.sortNo");
			query.setParamVal(appID, userID);
			List<BizObject> list = query.getResultList();
			for (BizObject bo : list)
			{
				AppItem item = new AppItem();
				BeanEntityFactory.getInstance().getObject(item, bo);
				itemList.add(item);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return itemList;
	}
}
